package player.ui.dialog;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.JComponent;

public class ImgPanel extends JComponent{
	private static final long serialVersionUID = 2372863977940555623L;
	BufferedImage[]  bis;
	Color bgColor;
	boolean  selected;
	File[]  fs;
	public static int an=0;
	int xOffset=2;
	
	public ImgPanel(BufferedImage[]  bi,Color bgColor,boolean  selected){
		this.bgColor=bgColor;
		this.selected=selected;
		this.bis=bi;
	}
	
	public void paint(Graphics g){
		if(bis==null){
			return;
		}
		Graphics2D g2=(Graphics2D)g;
		int gapX=1;
		int width=100;
		if(!selected){
			width=45;
			g2.setColor(bgColor);
			for(int i=0;i<bis.length;i++){
				BufferedImage bi2 = bis[i];
				if(bi2!=null){
					float rate=(float)bi2.getWidth()/bi2.getHeight();
					int height=(int) ((float)width/rate);
					int yOffset=(MyTableCellRenderer0.rowHeight1-height)/2;
					g2.drawImage(bi2,
							xOffset,yOffset,width,height,
							bgColor,this );
					xOffset+=gapX;
					xOffset+=width;
				}
			}
		}else if(selected){
			width=100;
			g2.setColor(bgColor);
			long l=System.currentTimeMillis()/500;
			if(bis.length>0){
				int imgNum=(int) (l%bis.length);
				BufferedImage bi2 = bis[imgNum];
				xOffset=gapX+(OpenDialog.tWidth1-bi2.getWidth()-2*gapX)/2;
				xOffset=gapX;
				if(bi2!=null){
					float rate=(float)bi2.getWidth()/bi2.getHeight();
					int height=(int) ((float)width/rate);
					int yOffset=(MyTableCellRenderer0.rowHeight2-height)/2;
					g2.drawImage(bi2,xOffset,yOffset,width,height,bgColor,this );
					xOffset+=gapX;
					xOffset+=width;
				}
			}
		}
	}
	
	BufferedImage scale(Image src, int iHeight) {
		  float rate=(src.getWidth(this)/src.getHeight(this));
		  int nW=(int) (iHeight*rate);
	        int type = BufferedImage.TYPE_INT_RGB;
	        BufferedImage dst = new BufferedImage(nW, iHeight, type);
	        Graphics2D g2 = dst.createGraphics();
	        g2.drawImage(src, 0, 0, nW, iHeight, this);
	        g2.dispose();
	        return (dst);
	    }
	  
}
